package com.woniu.car.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.woniu.car.common.ResponseEntity;
import com.woniu.car.config.MyRedisTemplate;
import com.woniu.car.dto.StatisticsPlatformDto;
import com.woniu.car.entity.TStatisticsPlatform;
import com.woniu.car.exception.StatisticsException;
import com.woniu.car.mapper.TStatisticsPlatformMapper;
import com.woniu.car.parameter.StatisticsPlatformParameter;
import com.woniu.car.properties.StatisticsProperties;
import com.woniu.car.service.TStatisticsPlatformService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author wuyanzu
 * @since 2021-06-22
 */
@Slf4j
@Service
public class TStatisticsPlatformServiceImpl extends ServiceImpl<TStatisticsPlatformMapper, TStatisticsPlatform> implements TStatisticsPlatformService {
    //本日就查询redis,有参数就查询mysql数据库(查询月份)
    //统计平台各个商品收益且单独把总收益标注出来返回给前端 日/月
    //为空就redis中查询平台今日所有的商品消息
    //不为空,根据前端传来的数据,查询mysql数据库,查询t_statistics_platform表,条件为time=20212020
    @Resource
    private MyRedisTemplate myRedisTemplate;
    @Override
    public ResponseEntity StatisticsPlatform(StatisticsPlatformParameter parameter) throws StatisticsException {
        
        if(parameter.getMonth()==null&&parameter.getYear()==null){
            if(myRedisTemplate.opsForHash().size(StatisticsProperties.StatisticsPlatform)==0)
                throw new StatisticsException(StatisticsProperties.NOGOODSMSG);
            List<Object> list = myRedisTemplate.opsForHash().values(StatisticsProperties.StatisticsPlatform);
            ArrayList arrayList = new ArrayList<StatisticsPlatformDto>();
            for(Object obj:list){
                StatisticsPlatformDto dto = new StatisticsPlatformDto();
                TStatisticsPlatform platform = (TStatisticsPlatform) obj;
                BeanUtils.copyProperties(platform,dto);
                arrayList.add(dto);
            }
            return new ResponseEntity()
                    .setCode(StatisticsProperties.SUCCESSCODE)
                    .setMsg(StatisticsProperties.SUCCESSMSG)
                    .setObject(arrayList);
        }else{
            //查询数据库
            String month = parameter.getMonth().toString();
            if(parameter.getMonth()<10)
                month = "0" + month;
            List<TStatisticsPlatform> list = list(new QueryWrapper<TStatisticsPlatform>()
                    .ge("time", parameter.getYear().toString() + month + "01")
                    .le("time", parameter.getYear().toString() + month + "31")
            );
            //若集合中无信息则报错
            if(list.size()==0)
                throw new StatisticsException(StatisticsProperties.NOGOODSMSG);
            log.info("查询成功");
            return new ResponseEntity(StatisticsProperties.SUCCESSCODE,StatisticsProperties.SUCCESSMSG,list);
        }
            
        
            
        
    }

    @Override
    public void add(TStatisticsPlatform tStatisticsPlatform) {
        myRedisTemplate.opsForHash().put(StatisticsProperties.StatisticsPlatform,tStatisticsPlatform.getGoodId().toString(),tStatisticsPlatform);
    }
}
